package com.app.controller.backstage;

import com.app.model.AdminUserModel;
import com.app.service.AdminUserService;
import com.app.service.SubsidyApplicationService;
import com.app.util.Result;
import com.app.util.TokenUtils;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Date;

@RestController
@RequestMapping("/adminSubsidyApplication")
@Slf4j
public class AdminSubsidyApplicationController {

    @Autowired
    private SubsidyApplicationService subsidyApplicationService;
    @Autowired
    AdminUserService adminUserService;

    /**
     * 管理后台————大学生就业补贴列表查询（分页、条件）
     */
    @GetMapping("/adminList")
    public Result adminList(@RequestParam(required = false, defaultValue = "1") Integer page, @RequestParam(required = false, defaultValue = "10") Integer limit,
                            @RequestParam(required = false) Integer applicationState,
                            @RequestParam(required = false) String startTime, @RequestParam(required = false) String endTime, HttpServletRequest request) {
        String token = request.getHeader("token");
        String userId = TokenUtils.nothingness(token);
        if (userId == null) {
            return Result.error(405, "token不存在");
        }

        AdminUserModel user = adminUserService.getUserById(Integer.parseInt(userId));
        if (user == null) {
            return Result.error(405, "用户不存在");
        }
        return subsidyApplicationService.adminList(page, limit, applicationState, startTime, endTime);
    }

    /**
     * 管理后台————更改审核状态从待发放 到 已发放
     */
    @GetMapping("/updateListDateStatus")
    public Result updateListDateStatus(Integer subsidyApplicationId, HttpServletRequest request, String operator, String processingTime) throws ParseException {
        String token = request.getHeader("token");
        String userId = TokenUtils.nothingness(token);
        if (userId == null) {
            return Result.error(405, "token不存在");
        }

        AdminUserModel user = adminUserService.getUserById(Integer.parseInt(userId));
        if (user == null) {
            return Result.error(405, "用户不存在");
        }
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        Date date = dateFormat.parse(processingTime);
        return subsidyApplicationService.updateListDateStatus(subsidyApplicationId, user.getId(), operator, date);
    }


    /**
     * 管理后台————根据subsidyApplicationId查详情
     */
    @GetMapping("/getBySubsidyApplicationId")
    public Result getBySubsidyApplicationId(Integer subsidyApplicationId) {
        return subsidyApplicationService.getBySubsidyApplicationId(subsidyApplicationId);
    }

    /**
     * 详情的审核接口
     * type 4银行拒绝申请 5银行审核通过
     */
    @GetMapping("/subsidyApplicationDetailStatus")
    public Result SubsidyApplicationDetailStatus(Integer subsidyApplicationDetailId, Integer type, String reason, HttpServletRequest request) {
        String token = request.getHeader("token");
        String userId = TokenUtils.nothingness(token);
        if (userId == null) {
            return Result.error(405, "token不存在");
        }

        AdminUserModel user = adminUserService.getUserById(Integer.parseInt(userId));
        if (user == null) {
            return Result.error(405, "用户不存在");
        }
        return subsidyApplicationService.SubsidyApplicationDetailStatus(subsidyApplicationDetailId, type, reason, user.getId());
    }

    /**
     * 个人和企业信息审核
     * type 11个人信息通过 12个人信息拒绝 13企业信息通过 14企业信息拒绝
     */
    @GetMapping("/statusPOrE")
    public Result statusPOrE(Integer type, Integer pOrEId, String reason, HttpServletRequest request, Integer subsidyApplicationDetailId) {
        String token = request.getHeader("token");
        String userId = TokenUtils.nothingness(token);
        if (userId == null) {
            return Result.error(405, "token不存在");
        }

        AdminUserModel user = adminUserService.getUserById(Integer.parseInt(userId));
        if (user == null) {
            return Result.error(405, "用户不存在");
        }
        return subsidyApplicationService.statusPOrE(type, pOrEId, reason, user.getId(), subsidyApplicationDetailId);
    }

}
